<template>
	<view class="stock-up full box relative bg-primary flex-col">
		<view class="stock-box">
			<view class="stock-item">
				<text class="item-value">1</text>
				<text class="item-label">备货库存</text>
			</view>
			<view class="item-line"></view>
			<view class="stock-item" @click="handleToRecord">
				<image src="/static/img/stock_up_record.png" class="item-icon" mode="aspectFill"></image>
				<text class="item-label">备货记录</text>
			</view>
		</view>
		<view class="w-wrap">
			<view class="w-inner">
				<mescroll-uni @init="mescrollInit" :fixed="false" @down="downCallback" @up="upCallback">
					<view class="record-list">
						<view class="record-item" v-for="(item,index) in list" :key="index">
							<view class="item-header">
								<text class="item-circle"></text>
								<text class="device-name">{{ item.name }}</text>
							</view>
							<view class="item-details">
								<image :src="item.image" class="item-image" mode="aspectFill"></image>
								<view class="item-info">
									<!-- <text class="pack-name">{{ item.name }}</text> -->
									<view class="pack-content">
										<text class="common-text">套餐内容：</text>
										<text class="active-text">{{item.taocan_neirong}}</text>
										<!-- <text class="common-text">[0.28元/升]</text> -->
									</view>
									<text class="desc-item">时限：{{item.day_num}}天</text>
									<view class="item-price">
										<text class="price-label">备货价：</text>
										<text class="price-value">¥{{ item.pay_price }}</text>
									</view>
									<view class="item-number-box">
										<image class="number-icon" src="/static/icon/decrement_icon.png"
											mode="aspectFill" @click="handleAddCount(item,-1)" />
										<text class="number-value">{{ item.count }}</text>
										<image class="number-icon" src="/static/icon/increment_icon.png"
											mode="aspectFill" @click="handleAddCount(item,1)"></image>
									</view>
								</view>
							</view>
						</view>
					</view>
				</mescroll-uni>
			</view>
		</view>
		<view class="page-footer">
			<view class="total-price">
				<text>合计：</text>
				<text class="total-value">￥{{ totalPrice }}</text>
			</view>
			<view class="confirm-btn" @click="handleConfirm">
				<text>结算</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { request, showLoading, toast } from "@/utils/tool.js";
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import currency from "currency.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				list: [],
			}
		},
		computed: {
			totalPrice() {
				return this.list.reduce((pre, cur) => {
					const value = currency(cur.count).multiply(cur.pay_price).value
					return currency(pre).add(value).value
				}, 0)
			}
		},
		methods: {
			handleToRecord() {
				uni.navigateTo({
					url: "/pages/index/join-investment/stock-record"
				})
			},
			async handleConfirm() {
				const list = this.list.filter(item => item.count > 0)
				if (!list.length) return toast({ title: "请至少购买一个" })
				const hide = showLoading("正在下单")
				const resp = await request({
					url: "/beihuo/beihuosubmit",
					data: {
						json: JSON.stringify(list.map(item => {
							const { count, id } = item
							return { id, num: count }
						}))
					}
				}).finally(hide)
				console.log(resp, 'resp');
				if (resp.code !== 1) {
					return toast({ title: resp.msg || "下单失败" })
				}
				resp.data.order_amount = this.totalPrice
				uni.navigateTo({
					url: "/pages/pay/index?data=" + JSON.stringify(resp.data)
				})
			},
			handleAddCount(rowItem, value) {
				const count = Math.max(0, rowItem.count + value)
				rowItem.count = count
			},
			async upCallback(ev) {
				const { num, size } = ev
				const resp = await request({
					url: "/beihuo/beihuolists",
					method: "GET",
					data: {
						page_no: num,
						page_size: size,
					}
				})
				const { lists, count } = resp.data
				if (num === 1) this.list = []
				this.list.push(...lists.map(item => {
					item.count = 0
					return item
				}))
				this.mescroll.endBySize(lists.length, count)
				console.log(lists, 'lists');
			}
		},
	}
</script>

<style lang="scss" scoped>
	.stock-up {
		padding-top: 24rpx;

		.page-footer {
			padding: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background: #fff;

			.confirm-btn {
				width: 392rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				background: linear-gradient(90deg, #7AA4E8 0%, #0A67FD 100%);
				border-radius: 100rpx 100rpx 100rpx 100rpx;
				font-weight: bold;
				font-size: 28rpx;
				color: #FFFFFF;
			}

			.total-price {
				font-size: 28rpx;
				color: rgba(0, 0, 0, 0.9);
				line-height: 44rpx;

				.total-value {
					color: #FF4A18;
					font-weight: bold;
					font-size: 34rpx;
				}
			}
		}

		.record-list {
			padding-top: 24rpx;

			.record-item {
				padding: 20rpx 46rpx;
				background: #fff;
				margin-bottom: 22rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.item-details {
					display: flex;
					align-items: flex-start;
					margin-top: 16rpx;

					.item-number-box {
						padding: 0 10rpx;
						display: flex;
						align-items: center;
						margin-top: 8rpx;

						.number-icon {
							width: 32rpx;
							height: 32rpx;
						}

						.number-value {
							font-weight: 400;
							font-size: 24rpx;
							color: rgba(0, 0, 0, 0.9);
							line-height: 40rpx;
							width: 120rpx;
							text-align: center;
						}
					}

					.item-info {
						margin-left: 20rpx;
						display: flex;
						flex-flow: column;

						.desc-item {
							font-size: 22rpx;
							color: #737373;
							line-height: 44rpx;
						}

						.item-price {
							font-size: 26rpx;
							color: #737373;
							line-height: 44rpx;

							.price-value {
								color: #FF0000;
							}
						}

						.pack-content {
							font-weight: 400;
							font-size: 22rpx;
							line-height: 44rpx;

							.active-text {
								color: #FF0000;
							}

							.common-text {
								color: #737373;
							}
						}

						.pack-name {
							font-size: 28rpx;
							color: #515151;
							line-height: 44rpx;
						}
					}

					.item-image {
						width: 160rpx;
						height: 177rpx;
					}
				}

				.item-header {
					display: flex;
					align-items: center;
					margin-bottom: 2rpx;

					.item-circle {
						width: 12rpx;
						height: 12rpx;
						background: #26CF1F;
						border-radius: 50%;
					}

					.device-name {
						font-weight: 400;
						font-size: 28rpx;
						color: #4C4C4C;
						line-height: 44rpx;
						margin-left: 8rpx;
					}
				}
			}
		}

		.stock-box {
			padding: 22rpx 140rpx 12rpx;
			box-sizing: border-box;
			position: relative;
			background: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.item-line {
				width: 2rpx;
				height: 74rpx;
				background: #F0F0F0;
			}

			.stock-item {
				display: flex;
				align-items: center;
				flex-flow: column;

				.item-label {
					font-size: 24rpx;
					color: #3361F0;
					line-height: 44rpx;
				}

				.item-value {
					font-weight: bold;
					font-size: 36rpx;
					color: #454545;
					line-height: 44rpx;
				}

				.item-icon {
					width: 34rpx;
					height: 34rpx;
					margin-bottom: 2rpx;
				}
			}
		}
	}
</style>